library(zipcodeR)
library(ggplot2)
library(maps)
library(huxtable)
library(officer)
library(knitr)
library(broom)
library(tidyr)
library(stargazer)

######################################
######################################
######################################
######################################
######### APPENDIX B
######################################
######################################
################ B1
######################################
######################################
PNAS_Appendix_B1_black<- lm(Support_for_political_violence ~ 
                              Diversification  * county_change_in_pct_black
                              + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                              data = study1_PNAS_yougov_WHITE,
                              weights = weight)
summary(PNAS_Appendix_B1_black)
PNAS_Appendix_B1_white<- lm(Support_for_political_violence ~ 
                              Diversification  * county_change_in_pct_white
                              + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                              data = study1_PNAS_yougov_WHITE,
                              weights = weight)
summary(PNAS_Appendix_B1_white)
PNAS_Appendix_B1_hispanic<- lm(Support_for_political_violence ~ 
                                 Diversification  * county_change_in_pct_hispanic
                                 + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                 data = study1_PNAS_yougov_WHITE,
                                 weights = weight)
summary(PNAS_Appendix_B1_hispanic)
PNAS_Appendix_B1_asian<- lm(Support_for_political_violence ~ 
                              Diversification  * county_change_in_pct_asian
                              + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                              data = study1_PNAS_yougov_WHITE,
                              weights = weight)
summary(PNAS_Appendix_B1_asian)

######################################
######################################
################ APPENDIX B2
######################################
######################################

PNAS_Appendix_B2_black <- lm(Racial_resentment_scale ~ 
                               Diversification  * county_change_in_pct_black
                        + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                        data = study1_PNAS_yougov_WHITE,
                        weights = weight)
summary(PNAS_Appendix_B2_black)
PNAS_Appendix_B2_white <- lm(Racial_resentment_scale ~
                               Diversification  * county_change_in_pct_white
                        + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                        data = study1_PNAS_yougov_WHITE,
                        weights = weight)
summary(PNAS_Appendix_B2_white)
PNAS_Appendix_B2_hispanic <- lm(Racial_resentment_scale ~ 
                                  Diversification  * county_change_in_pct_hispanic
                           + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                           data = study1_PNAS_yougov_WHITE,
                           weights = weight)
summary(PNAS_Appendix_B2_hispanic)
PNAS_Appendix_B2_asian <- lm(Racial_resentment_scale ~ 
                               Diversification  * county_change_in_pct_asian
                        + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                        data = study1_PNAS_yougov_WHITE,
                        weights = weight)
summary(PNAS_Appendix_B2_asian)


######################################
######################################
################ APPENDIX B3
######################################
######################################
ss = sim_slopes(PNAS_Appendix_B1_black,
                pred =Diversification, 
                modx = county_change_in_pct_black,
                modx.values = c(-.125, -0.1,-0.075, 
                                -0.05, -0.025, 
                                0, 0.025, 0.05, 
                                0.075, 0.1, .125))
plot(ss)
ss
as_hux(ss,
       sig.levels = c(`****` = 0.001, 
                      `***` = 0.01, 
                      `**` = 0.05, 
                      `*` = 0.1),)
johnson_neyman(PNAS_Appendix_B1_black, 
               pred =Diversification, 
               modx = county_change_in_pct_black, 
               alpha = .05)
######################################
######################################
################ APPENDIX B4
######################################
######################################
PNAS_Appendix_B4_black<- lm( national_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_black
                                         + EDU + INCOME + PID + national_demographic_change
                                         , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B4_black)
PNAS_Appendix_B4_white<- lm( national_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_white
                             + EDU + INCOME + PID + national_demographic_change
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B4_white)
PNAS_Appendix_B4_hispanic<- lm( national_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_hispanic
                             + EDU + INCOME + PID + national_demographic_change
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B4_hispanic)
PNAS_Appendix_B4_asian<- lm( national_demographic_change_post ~ 
                                  diversification_prime*county_change_in_pct_asian
                                + EDU + INCOME + PID + national_demographic_change
                                , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B4_asian)

######################################
######################################
################ APPENDIX B5
######################################
######################################

PNAS_Appendix_B5_black<- lm( local_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_black
                             + EDU + INCOME + PID + IDEO_pre + local_demographic_change
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B5_black)
PNAS_Appendix_B5_white<- lm( local_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_white
                             + EDU + INCOME + PID + IDEO_pre + local_demographic_change
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B5_white)
PNAS_Appendix_B5_hispanic<- lm( local_demographic_change_post ~ 
                                  diversification_prime*county_change_in_pct_hispanic
                                + EDU + INCOME + PID + IDEO_pre + local_demographic_change
                                , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B5_hispanic)
PNAS_Appendix_B5_asian<- lm( local_demographic_change_post ~ 
                               diversification_prime*county_change_in_pct_asian
                             + EDU + INCOME + PID + IDEO_pre + local_demographic_change
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B5_hispanic)

######################################
######################################
################ APPENDIX B6
######################################
######################################
PNAS_Appendix_B6_black<- lm( PID_post ~ 
                               diversification_prime*county_change_in_pct_black
                             + EDU + INCOME + PID + IDEO_pre 
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B6_black)
PNAS_Appendix_B6_white<- lm( PID_post ~ 
                               diversification_prime*county_change_in_pct_white
                             + EDU + INCOME + PID + IDEO_pre 
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B6_white)
PNAS_Appendix_B6_hispanic<- lm( PID_post ~ 
                                  diversification_prime*county_change_in_pct_hispanic
                                + EDU + INCOME + PID + IDEO_pre 
                                , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B6_hispanic)
PNAS_Appendix_B6_asian<- lm( PID_post ~ 
                               diversification_prime*county_change_in_pct_asian
                             + EDU + INCOME + PID + IDEO_pre 
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B6_asian)

######################################
######################################
################ APPENDIX B7
######################################
######################################
PNAS_Appendix_B7_black_BLACK_THREAT<- lm( black_FUTURE_threat_pre ~ 
                               diversification_prime*county_change_in_pct_black
                             + EDU + INCOME + PID  
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_black_BLACK_THREAT)
PNAS_Appendix_B7_white_BLACK_THREAT<- lm(black_FUTURE_threat_pre ~ 
                               diversification_prime*county_change_in_pct_white
                             + EDU + INCOME + PID  
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_white_BLACK_THREAT)
PNAS_Appendix_B7_hispanic_BLACK_THREAT<- lm( black_FUTURE_threat_pre ~ 
                                  diversification_prime*county_change_in_pct_hispanic
                                + EDU + INCOME + PID  
                                , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_hispanic_BLACK_THREAT)
PNAS_Appendix_B7_asian_BLACK_THREAT<- lm( black_FUTURE_threat_pre ~ 
                               diversification_prime*county_change_in_pct_asian
                             + EDU + INCOME + PID  
                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_asian_BLACK_THREAT)

PNAS_Appendix_B7_black_ASIAN_THREAT<- lm(asian_FUTURE_threat_pre ~ 
                                            diversification_prime*county_change_in_pct_black
                                          + EDU + INCOME + PID  
                                          , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_black_ASIAN_THREAT)
PNAS_Appendix_B7_white_ASIAN_THREAT<- lm(asian_FUTURE_threat_pre ~ 
                                           diversification_prime*county_change_in_pct_white
                                         + EDU + INCOME + PID  
                                         , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_white_ASIAN_THREAT)
PNAS_Appendix_B7_hispanic_ASIAN_THREAT<- lm( asian_FUTURE_threat_pre ~ 
                                               diversification_prime*county_change_in_pct_hispanic
                                             + EDU + INCOME + PID  
                                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_hispanic_ASIAN_THREAT)
PNAS_Appendix_B7_asian_ASIAN_THREAT<- lm( asian_FUTURE_threat_pre ~ 
                                            diversification_prime*county_change_in_pct_asian
                                          + EDU + INCOME + PID  
                                          , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B7_asian_ASIAN_THREAT)


######################################
######################################
################ APPENDIX B8
######################################
######################################

PNAS_Appendix_B8_black_LATINO_THREAT<- lm( latinx_FUTURE_threat_pre ~ 
                                             diversification_prime*county_change_in_pct_black
                                           + EDU + INCOME + PID  
                                           , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_black_LATINO_THREAT)
PNAS_Appendix_B8_white_LATINO_THREAT<- lm(latinx_FUTURE_threat_pre ~ 
                                            diversification_prime*county_change_in_pct_white
                                          + EDU + INCOME + PID  
                                          , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_white_LATINO_THREAT)
PNAS_Appendix_B8_hispanic_LATINO_THREAT<- lm( latinx_FUTURE_threat_pre ~ 
                                                diversification_prime*county_change_in_pct_hispanic
                                              + EDU + INCOME + PID  
                                              , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_hispanic_LATINO_THREAT)
PNAS_Appendix_B8_asian_LATINO_THREAT<- lm( latinx_FUTURE_threat_pre ~ 
                                             diversification_prime*county_change_in_pct_asian
                                           + EDU + INCOME + PID  
                                           , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_asian_LATINO_THREAT)

PNAS_Appendix_B8_black_IMMIGRANT_THREAT<- lm( immigrants_FUTURE_threat_pre ~ 
                                                diversification_prime*county_change_in_pct_black
                                              + EDU + INCOME + PID  
                                              , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_black_IMMIGRANT_THREAT)
PNAS_Appendix_B8_white_IMMIGRANT_THREAT<- lm(immigrants_FUTURE_threat_pre ~ 
                                               diversification_prime*county_change_in_pct_white
                                             + EDU + INCOME + PID  
                                             , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_white_IMMIGRANT_THREAT)
PNAS_Appendix_B8_hispanic_IMMIGRANT_THREAT<- lm( immigrants_FUTURE_threat_pre ~ 
                                                   diversification_prime*county_change_in_pct_hispanic
                                                 + EDU + INCOME + PID  
                                                 , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_hispanic_IMMIGRANT_THREAT)
PNAS_Appendix_B8_asian_IMMIGRANT_THREAT<- lm( immigrants_FUTURE_threat_pre ~ 
                                                diversification_prime*county_change_in_pct_asian
                                              + EDU + INCOME + PID  
                                              , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B8_asian_IMMIGRANT_THREAT)



######################################
######################################
################ APPENDIX B9
######################################
######################################
PNAS_Appendix_B9.1 <- lm( worse_if_a_rep_was_MURDERED ~ 
                            diversification_prime*county_change_in_pct_black 
                        + EDU + INCOME + PID 
                        , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B9.1)
PNAS_Appendix_B9.2<- lm( worse_if_a_rep_was_MURDERED ~ 
                           diversification_prime*county_change_in_pct_white
                                         + EDU + INCOME + PID  
                                         , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B9.2)
PNAS_Appendix_B9.3 <- lm( worse_if_a_rep_was_MURDERED ~ 
                            diversification_prime*county_change_in_pct_hispanic 
                          + EDU + INCOME + PID 
                          , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B9.3)
PNAS_Appendix_B9.4<- lm( worse_if_a_rep_was_MURDERED ~ 
                           diversification_prime*county_change_in_pct_asian
                         + EDU + INCOME + PID  
                         , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_B9.4)


######################################
######################################
################ APPENDIX B11 and B10
######################################
######################################
PNAS_Appendix_B11.1 <- lm(Violence_support ~ 
                                      national_VS_state_threat*county_change_in_pct_white
                                    + Violence_support_pre + PID_pre + EDU + INCOME + GENDER, 
                                    data = state_threat_white_change_white_only1)
summary(PNAS_Appendix_B11.1)
PNAS_Appendix_B11.2 <- lm(Violence_support ~ 
                                      national_VS_state_threat*county_change_in_pct_black
                          + Violence_support_pre + PID_pre + EDU + INCOME + GENDER, 
                                    data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B11.2)
PNAS_Appendix_B11.3 <- lm(Violence_support ~ 
                                     national_VS_state_threat*county_change_in_pct_hispanic
                          + Violence_support_pre + PID_pre + EDU + INCOME + GENDER,  
                                   data = state_threat_white_change_white_only1)
summary(PNAS_Appendix_B11.3)
PNAS_Appendix_B11.4 <- lm(Violence_support ~ 
                                      national_VS_state_threat*county_change_in_pct_asian
                                    + Violence_support_pre + PID_pre + EDU + INCOME + GENDER, 
                                    data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B11.4)

state_prime1= interact_plot(PNAS_Appendix_B11.1,
                            pred =county_change_in_pct_white, 
                            modx = national_VS_state_threat, interval = T,
                            x.label = "Change in White population percentage, county-level",
                            y.label = "Support for Political Violence",
                            modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                            legend.main = "",
                            colors = "CUD Bright")
state_prime2 <-interact_plot(PNAS_Appendix_B11.2, 
                             pred =county_change_in_pct_black, 
                             modx = national_VS_state_threat, interval = T,
                             x.label = "Change in Black population percentage, county-level",
                             y.label = "",
                             modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                             legend.main = "",
                             colors = "CUD Bright")
state_prime3 <- interact_plot(PNAS_Appendix_B11.3, 
                              pred =county_change_in_pct_hispanic, 
                              modx = national_VS_state_threat, interval = T,
                              x.label = "Change in Hispanic population percentage, county-level",
                              y.label = "Support for Political Violence",
                              modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                              legend.main = "",
                              colors = "CUD Bright")
state_prime4 <- interact_plot(PNAS_Appendix_B11.4, 
                              pred =county_change_in_pct_asian, 
                              modx = national_VS_state_threat, interval = T,
                              x.label = "Change in Asian population percentage, county-level",
                              y.label = "",
                              modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                              legend.main = "",
                              colors = "CUD Bright")

######################################
######################################
################ APPENDIX B12
######################################
######################################
PNAS_Appendix_B12.1 <- lm(Violence_support ~national_threat*county_change_in_pct_white
                    + Violence_support_pre 
                    + EDU + PID_pre + INCOME + GENDER,
                    data = state_threat_white_change_white_only1)
summary(PNAS_Appendix_B12.1)
PNAS_Appendix_B12.2 <- lm(Violence_support ~ national_threat*county_change_in_pct_black
                    + Violence_support_pre
                    + EDU + PID_pre + INCOME + GENDER,
                    data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B12.2)
PNAS_Appendix_B12.3 <- lm(Violence_support ~ national_threat*county_change_in_pct_hispanic
                    + Violence_support_pre 
                    + EDU + PID_pre + INCOME + GENDER,
                    data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B12.3)
PNAS_Appendix_B12.4 <- lm(Violence_support ~ national_threat*county_change_in_pct_asian
                    + Violence_support_pre
                    + EDU + PID_pre + INCOME + GENDER,
                    data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B12.4)

######################################
######################################
################ APPENDIX B14
######################################
######################################

PNAS_Appendix_B14.1 <- lm(Republican_feeling_therm ~national_threat*county_change_in_pct_white
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1)
summary(PNAS_Appendix_B14.1)
PNAS_Appendix_B14.2 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_black
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B14.2)
PNAS_Appendix_B14.3 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_hispanic
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B14.3)
PNAS_Appendix_B14.4 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_asian
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B14.4)



######################################
######################################
################ APPENDIX B15
######################################
######################################
PNAS_Appendix_B15.1 <- lm(Republican_feeling_therm ~national_threat*county_change_in_pct_white
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1)
summary(PNAS_Appendix_B15.1)
PNAS_Appendix_B15.2 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_black
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B15.2)
PNAS_Appendix_B15.3 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_hispanic
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B15.3)
PNAS_Appendix_B15.4 <- lm(Republican_feeling_therm ~ national_threat*county_change_in_pct_asian
                          + EDU + PID_pre + INCOME + GENDER,
                          data = state_threat_white_change_white_only1 )
summary(PNAS_Appendix_B15.4)




######################################
######################################
################ APPENDIX B17
######################################
######################################
PNAS_Appendix_B17.1= lm(Support_for_violence ~ 
                          correct_poc *county_change_in_pct_black +
                 Support_for_violence_pre +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE)
summary(PNAS_Appendix_B17.1)
PNAS_Appendix_B17.2= lm(Support_for_violence ~ 
                          correct_poc *county_change_in_pct_white +
                 Support_for_violence_pre  +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B17.2)
PNAS_Appendix_B17.3= lm(Support_for_violence ~ 
                          correct_poc *county_change_in_pct_hispanic +
                 Support_for_violence_pre +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B17.3)
PNAS_Appendix_B17.4= lm(Support_for_violence ~ 
                          correct_poc *county_change_in_pct_asian +
                 Support_for_violence_pre +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B17.4)

######################################
######################################
################ APPENDIX B18
######################################
######################################

PNAS_Appendix_B18.1= lm(hard_to_trust_elections ~ 
                          correct_poc *county_change_in_pct_black +
                          PID_pre + EDU + INCOME,
                        data = prime_corrections_white_fight_WHITE)
summary(PNAS_Appendix_B18.1)
PNAS_Appendix_B18.2= lm(hard_to_trust_elections ~ 
                          correct_poc *county_change_in_pct_white +
                          PID_pre + EDU + INCOME,
                        data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B18.2)
PNAS_Appendix_B18.3= lm(hard_to_trust_elections ~ 
                          correct_poc *county_change_in_pct_hispanic +
                          PID_pre + EDU + INCOME,
                        data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B18.3)
PNAS_Appendix_B18.4= lm(hard_to_trust_elections ~ 
                          correct_poc *county_change_in_pct_asian +
                          PID_pre + EDU + INCOME,
                        data = prime_corrections_white_fight_WHITE )
summary(PNAS_Appendix_B18.4)



# Filter the dataset to exclude Southern states and select desired columns
### Recoding states
southern_states <- c(
  "Alabama", "Arkansas", "Florida", "Georgia", "Kentucky", "Louisiana", 
  "Mississippi", "North Carolina", "Oklahoma", "South Carolina", "Tennessee", 
  "Texas", "Virginia", "West Virginia", "Maryland", "Delaware")

# Define states in the Midwest
midwestern_states <- c(
  "Illinois", "Indiana", "Iowa", "Kansas", "Michigan", 
  "Minnesota", "Missouri", "Nebraska", "North Dakota", 
  "Ohio", "South Dakota", "Wisconsin"
)
# Define states in the West
western_states <- c(
  "Alaska", "Arizona", "California", "Colorado", 
  "Hawaii", "Idaho", "Montana", "Nevada", 
  "New Mexico", "Oregon", "Utah", 
  "Washington", "Wyoming"
)
# Define states in the Northeast
northeastern_states <- c(
  "Connecticut", "Maine", "Massachusetts", 
  "New Hampshire", "Rhode Island", 
  "Vermont", "New Jersey",
  "New York", 
  "Pennsylvania", "Maryland", "Delaware"
)
############################
############################
####### FILTER DATASETS
############################
############################

# write.csv(prime_corrections_white_fight_WHITE_NO_south, "study4_prolific_white_no_south.csv")

######################################
######################################
################ APPENDIX C 
######################################
######################################
study2_PNAS_LUCID_white_NO_SOUTH <- study2_PNAS_LUCID_white %>%
  filter(!region %in% "South") %>%
  select(StartDate:local_demographic_change_post)
PNAS_Appendix_C.1 <- lm(worse_if_a_rep_was_MURDERED ~ 
                         diversification_prime*county_change_in_pct_black  
                       + EDU + INCOME + PID + IDEO_pre
                       , data = study2_PNAS_LUCID_white)
summary(PNAS_Appendix_C.1)
PNAS_Appendix_C.2 <- lm(worse_if_a_rep_was_MURDERED ~ 
                         diversification_prime*county_change_in_pct_black  
                       + EDU + INCOME + PID + IDEO_pre
                       , data = study2_PNAS_LUCID_white_NO_SOUTH)
summary(PNAS_Appendix_C.2)

# Filter the data set to exclude Southern states and select desired columns
prime_corrections_white_fight_WHITE_NO_south <- prime_corrections_white_fight_WHITE %>%
  filter(!STATE %in% southern_states) %>%
  select(StartDate:HispanicΔ)
PNAS_Appendix_C.3_FIGURES9 = lm(Support_for_violence ~ correct_poc *county_change_in_pct_black +
                         Support_for_violence_pre +
                         PID_pre + EDU + INCOME,
                       data = prime_corrections_white_fight_WHITE_NO_south)
summary(PNAS_Appendix_C.3_FIGURES9)
interact_plot(PNAS_Appendix_C.3_FIGURES9,
              pred =county_change_in_pct_black, 
              modx = correct_poc, interval = T ,
              x.label = "County Change in Black Population, 2011-21",
              y.label = "Support for Political Violence",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")


######################################
######################################
################ APPENDIX C3 
######################################
######################################

diversification_corpus_study1 <- study1_PNAS_yougov_WHITE %>% 
  mutate(
    black_mention = as.integer(str_detect(str_to_lower(Q18), "black|africa")),
    english_mention = as.integer(str_detect(str_to_lower(Q18), "english|spanish")),
    latin_mention = as.integer(str_detect(str_to_lower(Q18), "hispani|mexic|latin")),
    white_mention = as.integer(str_detect(str_to_lower(Q18), "white")),
    asian_mention = as.integer(str_detect(str_to_lower(Q18), "asia|china|chinese")),
    immigrant_mention = as.integer(str_detect(str_to_lower(Q18), "immigra")),
    democrat_mention = as.integer(str_detect(str_to_lower(Q18), "democrat")),
    republican_mention = as.integer(str_detect(str_to_lower(Q18), "republican")),
    minorities_mention = as.integer(str_detect(str_to_lower(Q18), "minoritie")))

diversification_corpus_study2 <- study2_PNAS_LUCID_white %>% 
  mutate(
    black_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "black|africa")),
    english_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "english|spanish")),
    latin_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "hispani|mexic|latin")),
    white_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "white")),
    asian_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "asia|china|chinese")),
    immigrant_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "immigra")),
    democrat_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "democrat")),
    republican_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "republican")),
    minorities_mention = as.integer(str_detect(str_to_lower(demo_prime_open), "minoritie")))

###########################################################################
###### TRUMP VOTE IN PREDICTED OUT OF RESPONDENT LEVEL DAT, lucid, study 2
###########################################################################
presidential_county_data_2020 <- dplyr::select(filter(pres_elections_release, 
                                                      election_year == 2020 ), 
                                               c(election_year:original_name_end_date))
presidential_county_data_2020$COUNTY= (presidential_county_data_2020$fips)
diversification_YOUGOV_white_2020_presidential = merge(x=diversification_yougov_white,
                                                       y=presidential_county_data_2020,by="COUNTY",all.x=TRUE)
diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020= (diversification_YOUGOV_white_2020_presidential$republican_raw_votes - 
                                                                            diversification_YOUGOV_white_2020_presidential$democratic_raw_votes)

diversification_YOUGOV_white_2020_presidential$Red_county <- 
  ifelse(diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020 > 2000, 1, 0)

# Blue counties: Vote_Trump_positive_2020 < -2000
diversification_YOUGOV_white_2020_presidential$Blue_county <- 
  ifelse(diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020 < -2000, 1, 0)

# Purple counties: -2000 <= Vote_Trump_positive_2020 <= 2000
diversification_YOUGOV_white_2020_presidential$Purple_county <- 
  ifelse(diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020 >= -2000 & 
           diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020 <= 2000, 1, 0)

diversification_LUCID_white_study2_2020 = merge(x=study2_lucid_corpus_with_sentiment,
                                                y=presidential_county_data_2020,
                                                by="COUNTY",all.x=TRUE)
diversification_LUCID_white_study2_2020$Vote_Trump_positive_2020= (diversification_LUCID_white_study2_2020$republican_raw_votes - 
                                                                     diversification_LUCID_white_study2_2020$democratic_raw_votes)
hist(diversification_YOUGOV_white_2020_presidential$Vote_Trump_positive_2020)
diversification_LUCID_white_study2_2020$Red_county <- 
  ifelse(diversification_LUCID_white_study2_2020$Vote_Trump_positive_2020 > 2000, 1, 0)
# Blue counties: Vote_Trump_positive_2020 < -2000
diversification_LUCID_white_study2_2020$Blue_county <- 
  ifelse(diversification_LUCID_white_study2_2020$Vote_Trump_positive_2020 < -2000, 1, 0)
# Purple counties: -2000 <= Vote_Trump_positive_2020 <= 2000
diversification_LUCID_white_study2_2020$Purple_county <- 
  ifelse(diversification_LUCID_white_study2_2020$Vote_Trump_positive_2020 >= -2000 & 
           diversification_LUCID_white_study2_2020$Vote_Trump_positive_2020 <= 2000, 1, 0)




##########################################
##########################################
#### CHECKING FOR EDU as a moderator
#### APPENDIX C4.EDU1-C4.EDU1
##########################################
##########################################
black_edu_county_predictors_triple_interaction<- lm(Support_political_violence ~ 
                                                      diversification_treatment*county_change_in_pct_black*EDU +
                                                      PID + GENDER + 
                                                      EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                      county_population_change +
                                                      pres_raw_county_vote_totals_two_party +
                                                      raw_county_vote_totals, 
                                                    data = diversification_YOUGOV_white_2020_presidential,
                                                    weights = weight)
summary(black_edu_county_predictors_triple_interaction)
interact_plot(black_edu_county_predictors_triple_interaction, 
              pred =EDU, 
              modx = diversification_treatment, 
              mod2 = county_change_in_pct_black,
              interval = T ,
              x.label = "Education",
              mod2.labels = c("Change in Black Population, -1 SD", "Change in Black Population, Mean", 
                              "Change in Black Population, +1 SD"),
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")
white_edu_county_predictors_triple_interaction<- lm(Support_political_violence ~ 
                                                      diversification_treatment*county_change_in_pct_white*EDU +
                                                      PID + GENDER + 
                                                      EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                      county_population_change +
                                                      pres_raw_county_vote_totals_two_party +
                                                      raw_county_vote_totals, 
                                                    data = diversification_YOUGOV_white_2020_presidential,
                                                    weights = weight)

summary(white_edu_county_predictors_triple_interaction)
interact_plot(white_edu_county_predictors_triple_interaction, 
              pred =EDU, 
              modx = diversification_treatment, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "Education",
              mod2.labels = c("Change in White Population, -1 SD", "Change in White Population, Mean", 
                              "Change in White Population, +1 SD"),
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")
hispanic_edu_county_predictors_triple_interaction<- lm(Support_political_violence ~ 
                                                         diversification_treatment*county_change_in_pct_hispanic*EDU +
                                                         PID + GENDER + 
                                                         EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                         county_population_change +
                                                         pres_raw_county_vote_totals_two_party +
                                                         raw_county_vote_totals, 
                                                       data = diversification_YOUGOV_white_2020_presidential,
                                                       weights = weight)
summary(hispanic_edu_county_predictors_triple_interaction)
interact_plot(hispanic_edu_county_predictors_triple_interaction, 
              pred =EDU, 
              modx = diversification_treatment, 
              mod2 = county_change_in_pct_hispanic,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")
asian_edu_county_predictors_triple_interaction<- lm(Support_for_political_violence ~ 
                                                      Diversification*county_change_in_pct_asian*EDU +
                                                      PID + GENDER + 
                                                      EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                      county_population_change,
                                                    data = study1_PNAS_yougov_WHITE,
                                                    weights = weight)
summary(asian_edu_county_predictors_triple_interaction)
interact_plot(asian_edu_county_predictors_triple_interaction, 
              pred =EDU, 
              modx = Diversification, 
              mod2 = county_change_in_pct_asian,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

ht <- tibble(white_edu_county_predictors_triple_interaction,
             black_edu_county_predictors_triple_interaction,
             hispanic_edu_county_predictors_triple_interaction,
             asian_edu_county_predictors_triple_interaction)
# Tidy the model and print as a table
tidy(white_edu_county_predictors_triple_interaction,
     black_edu_county_predictors_triple_interaction,
     hispanic_edu_county_predictors_triple_interaction,
     asian_edu_county_predictors_triple_interaction) %>%
  kable(digits = 3, col.names = c("Term", "Estimate", "Std. Error", "Statistic", "P-value"))


####################################################################
####################################################################
#### STUDY 2, EDU moderator
#### APPENDIX C4.EDU2-C4.EDU3
####################################################################
####################################################################
white_county_white_nationalism_county_predictors_LUCID <- lm(white_nationalism_THREAT ~  
                                                               diversification_prime* county_change_in_pct_white*EDU
                                                             + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                                               county_population +
                                                               county_population_change +
                                                               pres_raw_county_vote_totals_two_party +
                                                               raw_county_vote_totals, 
                                                             data = diversification_LUCID_white_study2_2020,)
interact_plot(white_county_white_nationalism_county_predictors_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################
black_county_white_nationalism_county_predictors_LUCID <- lm(white_nationalism_THREAT ~  
                                                               diversification_prime* county_change_in_pct_black*EDU
                                                             + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                                               county_population +
                                                               county_population_change +
                                                               pres_raw_county_vote_totals_two_party +
                                                               raw_county_vote_totals, 
                                                             data = study2_PNAS_LUCID_white,)
interact_plot(black_county_white_nationalism_county_predictors_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_black,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################
hispanic_county_white_nationalism_county_predictors_LUCID <- lm(white_nationalism_THREAT ~  
                                                                  diversification_prime* county_change_in_pct_hispanic*EDU
                                                                + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                                                  county_population +
                                                                  county_population_change, 
                                                                data = study2_PNAS_LUCID_white,)
interact_plot(hispanic_county_white_nationalism_county_predictors_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_hispanic,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")
####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################
asian_county_white_nationalism_county_predictors_LUCID <- lm(white_nationalism_THREAT ~  
                                                               diversification_prime* county_change_in_pct_asian*EDU
                                                             + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                                               county_population +
                                                               county_population_change +
                                                               pres_raw_county_vote_totals_two_party +
                                                               raw_county_vote_totals, 
                                                             data = diversification_LUCID_white_study2_2020,)
interact_plot(asian_county_white_nationalism_county_predictors_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_asian,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################

black_county_predictors_murder_LUCID <- lm(worse_if_a_rep_was_MURDERED ~  
                                             diversification_prime* county_change_in_pct_black*EDU
                                           + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                             county_population  + 
                                             county_population_change,
                                           data = study2_PNAS_LUCID_white,)
summary(black_county_predictors_murder_LUCID)
interact_plot(black_county_predictors_murder_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_black,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################

white_county_predictors_murder_LUCID <- lm(worse_if_a_rep_was_MURDERED ~  
                                             diversification_prime* county_change_in_pct_white*EDU
                                           + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                             county_population  + 
                                             county_population_change,
                                           data = study2_PNAS_LUCID_white,)
summary(white_county_predictors_murder_LUCID)
interact_plot(white_county_predictors_murder_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################
hispanic_county_predictors_murder_LUCID <- lm(worse_if_a_rep_was_MURDERED ~  
                                                diversification_prime* county_change_in_pct_hispanic*EDU
                                              + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                                county_population  + 
                                                county_population_change +
                                                pres_raw_county_vote_totals_two_party +
                                                raw_county_vote_totals, 
                                              data = study2_PNAS_LUCID_white,)
summary(hispanic_county_predictors_murder_LUCID)
interact_plot(hispanic_county_predictors_murder_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_hispanic,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
####################################################################
####################################################################
#### STUDY 2, EDU moderator
####################################################################
####################################################################

asian_county_predictors_murder_LUCID <- lm(worse_if_a_rep_was_MURDERED ~  
                                             diversification_prime* county_change_in_pct_asian*EDU
                                           + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                             county_population  + 
                                             county_population_change,
                                           data = study2_PNAS_LUCID_white,)
summary(asian_county_predictors_murder_LUCID)
interact_plot(asian_county_predictors_murder_LUCID, 
              pred =EDU, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_asian,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)



####################################################################
####################################################################
#### STUDY 4, EDU moderator
####################################################################
####################################################################
study4_PNAS_violence_black_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_white*EDU 
                                         + violence_necessary_pre 
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_white_fight_WHITE)
summary(study4_PNAS_violence_black_counties)
interact_plot(study4_PNAS_violence_black_counties, 
              pred =EDU, 
              modx = correct_poc, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("Demo Prime", "Demo Prime + POC Correction"),
              colors = "CUD Bright",)

####################################################################
####################################################################
#### STUDY 4, EDU moderator
####################################################################
####################################################################
study4_PNAS_violence_black_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_black*EDU 
                                         + violence_necessary_pre 
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_white_fight_WHITE)
summary(study4_PNAS_violence_black_counties)
interact_plot(study4_PNAS_violence_black_counties, 
              pred =EDU, 
              modx = correct_poc, 
              mod2 = county_change_in_pct_black,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("Demo Prime", "Demo Prime + POC Correction"),
              colors = "CUD Bright",)

####################################################################
####################################################################
#### STUDY 4, EDU moderator
####################################################################
####################################################################
study4_PNAS_violence_hispanic_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_hispanic*EDU 
                                         + violence_necessary_pre 
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_white_fight_WHITE)
summary(study4_PNAS_violence_hispanic_counties)
interact_plot(study4_PNAS_violence_hispanic_counties, 
              pred =EDU, 
              modx = correct_poc, 
              mod2 = county_change_in_pct_hispanic,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("Demo Prime", "Demo Prime + POC Correction"),
              colors = "CUD Bright",)

####################################################################
####################################################################
#### STUDY 4, EDU moderator
####################################################################
####################################################################
study4_PNAS_violence_asian_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_asian*EDU 
                                         + violence_necessary_pre 
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_white_fight_WHITE)
summary(study4_PNAS_violence_asian_counties)
interact_plot(study4_PNAS_violence_asian_counties, 
              pred =EDU, 
              modx = correct_poc, 
              mod2 = county_change_in_pct_asian,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("Demo Prime", "Demo Prime + POC Correction"),
              
              colors = "CUD Bright",)



####################################################################
####################################################################
#### STUDY 1, WHITE
## APPENDIX C4.COUNT2
####################################################################
####################################################################

county_predictors_vote_trump_STUDY1_RED<- lm(Support_political_violence ~ 
                                               diversification_treatment*county_change_in_pct_white*Red_county +
                                               PID + GENDER + 
                                               EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                               county_population_change +
                                               pres_raw_county_vote_totals_two_party +
                                               raw_county_vote_totals, 
                                             data = diversification_YOUGOV_white_2020_presidential,
                                             weights = weight)
summary(county_predictors_vote_trump_STUDY1_RED)
interact_plot(county_predictors_vote_trump_STUDY1_RED, 
              pred =county_change_in_pct_white, 
              modx = diversification_treatment, 
              mod2 = Red_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
#### STUDY 1, WHITE
## APPENDIX C4.COUNT1
####################################################################
county_predictors_vote_trump_STUDY1_BLUE<- lm(Support_political_violence ~ 
                                               diversification_treatment*county_change_in_pct_white*Blue_county +
                                               PID + GENDER + 
                                               EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                               county_population_change +
                                               pres_raw_county_vote_totals_two_party +
                                               raw_county_vote_totals, 
                                             data = diversification_YOUGOV_white_2020_presidential,
                                             weights = weight)
summary(county_predictors_vote_trump_STUDY1_BLUE)
interact_plot(county_predictors_vote_trump_STUDY1_BLUE, 
              pred =county_change_in_pct_white, 
              modx = diversification_treatment, 
              mod2 = Blue_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, WHITE
## APPENDIX C4.COUNT3
####################################################################
####################################################################
county_predictors_vote_trump_STUDY1_PURPLE<- lm(Support_political_violence ~ 
                                                diversification_treatment*county_change_in_pct_white*Purple_county +
                                                PID + GENDER + 
                                                EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                county_population_change +
                                                pres_raw_county_vote_totals_two_party +
                                                raw_county_vote_totals, 
                                              data = diversification_YOUGOV_white_2020_presidential,
                                              weights = weight)
summary(county_predictors_vote_trump_STUDY1_PURPLE)
interact_plot(county_predictors_vote_trump_STUDY1_PURPLE, 
              pred =county_change_in_pct_white, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, BLACK
## APPENDIX C4.COUNT2
####################################################################
####################################################################
black_county_predictors_vote_trump_STUDY1_RED<- lm(Support_political_violence ~ 
                                                  diversification_treatment*county_change_in_pct_black*Red_county +
                                                  PID + GENDER + 
                                                  EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                  county_population_change +
                                                  pres_raw_county_vote_totals_two_party +
                                                  raw_county_vote_totals, 
                                                data = diversification_YOUGOV_white_2020_presidential,
                                                weights = weight)
summary(black_county_predictors_vote_trump_STUDY1_RED)
interact_plot(black_county_predictors_vote_trump_STUDY1_RED, 
              pred =county_change_in_pct_black, 
              modx = diversification_treatment, 
              mod2 = Red_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, Black
## APPENDIX C4.COUNT1
####################################################################
####################################################################
black_county_predictors_vote_trump_STUDY1_BLUE<- lm(Support_political_violence ~ 
                                                     diversification_treatment*county_change_in_pct_black*Blue_county +
                                                     PID + GENDER + 
                                                     EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                     county_population_change +
                                                     pres_raw_county_vote_totals_two_party +
                                                     raw_county_vote_totals, 
                                                   data = diversification_YOUGOV_white_2020_presidential,
                                                   weights = weight)
summary(black_county_predictors_vote_trump_STUDY1_BLUE)
interact_plot(black_county_predictors_vote_trump_STUDY1_BLUE, 
              pred =county_change_in_pct_black, 
              modx = diversification_treatment, 
              mod2 = Blue_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")
####################################################################
####################################################################
#### STUDY 1, Black

## APPENDIX C4.COUNT3
####################################################################
####################################################################
black_county_predictors_vote_trump_STUDY1_PURPLE<- lm(Support_political_violence ~ 
                                                      diversification_treatment*county_change_in_pct_black*Purple_county +
                                                      PID + GENDER + 
                                                      EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                      county_population_change +
                                                      pres_raw_county_vote_totals_two_party +
                                                      raw_county_vote_totals, 
                                                    data = diversification_YOUGOV_white_2020_presidential,
                                                    weights = weight)
summary(black_county_predictors_vote_trump_STUDY1_PURPLE)
interact_plot(black_county_predictors_vote_trump_STUDY1_PURPLE, 
              pred =county_change_in_pct_black, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, Hispanic

## APPENDIX C4.COUNT2
####################################################################
####################################################################
hispanic_county_predictors_vote_trump_STUDY1_Red<- lm(Support_political_violence ~ 
                                                        diversification_treatment*county_change_in_pct_hispanic*Red_county +
                                                        PID + GENDER + 
                                                        EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                        county_population_change +
                                                        pres_raw_county_vote_totals_two_party +
                                                        raw_county_vote_totals, 
                                                      data = diversification_YOUGOV_white_2020_presidential,
                                                      weights = weight)
summary(hispanic_county_predictors_vote_trump_STUDY1_Red)
interact_plot(hispanic_county_predictors_vote_trump_STUDY1_Red, 
              pred =county_change_in_pct_hispanic, 
              modx = diversification_treatment, 
              mod2 = Red_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")


####################################################################
####################################################################
#### STUDY 1, Hispanic
## APPENDIX C4.COUNT1
####################################################################
####################################################################
hispanic_county_predictors_vote_trump_STUDY1_Blue<- lm(Support_political_violence ~ 
                                                        diversification_treatment*county_change_in_pct_hispanic*Blue_county +
                                                        PID + GENDER + 
                                                        EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                        county_population_change +
                                                        pres_raw_county_vote_totals_two_party +
                                                        raw_county_vote_totals, 
                                                      data = diversification_YOUGOV_white_2020_presidential,
                                                      weights = weight)
summary(hispanic_county_predictors_vote_trump_STUDY1_Blue)
interact_plot(hispanic_county_predictors_vote_trump_STUDY1_Blue, 
              pred =county_change_in_pct_hispanic, 
              modx = diversification_treatment, 
              mod2 = Blue_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")


####################################################################
####################################################################
#### STUDY 1, Hispanic
## APPENDIX C4.COUNT3
####################################################################
####################################################################
hispanic_county_predictors_vote_trump_STUDY1_Purple<- lm(Support_political_violence ~ 
                                                         diversification_treatment*county_change_in_pct_hispanic*Purple_county +
                                                         PID + GENDER + 
                                                         EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                         county_population_change +
                                                         pres_raw_county_vote_totals_two_party +
                                                         raw_county_vote_totals, 
                                                       data = diversification_YOUGOV_white_2020_presidential,
                                                       weights = weight)
summary(hispanic_county_predictors_vote_trump_STUDY1_Purple)
interact_plot(hispanic_county_predictors_vote_trump_STUDY1_Purple, 
              pred =county_change_in_pct_hispanic, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")







####################################################################
####################################################################
#### APPENDIX E1
## Controlling for all counties for each interaction, white county
####################################################################
####################################################################
controlling_for_all_STUDY1_WHITE_VIOLENCE<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_white +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals 
                                  +county_change_in_pct_black +
                                    county_change_in_pct_hispanic +
                                    county_change_in_pct_asian, 
                                  data = diversification_YOUGOV_white_2020_presidential,
                                  weights = weight)
summary(controlling_for_all_STUDY1_WHITE_VIOLENCE)

####################################################################
####################################################################
#### APPENDIX E1
## Controlling for all counties for each interaction, black county
####################################################################
####################################################################
controlling_for_all_STUDY1_BLACK_VIOLENCE<- lm(Support_political_violence ~ 
                                                 diversification_treatment*county_change_in_pct_black +
                                                 PID + GENDER + 
                                                 EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                 county_population_change +
                                                 pres_raw_county_vote_totals_two_party +
                                                 raw_county_vote_totals 
                                               +county_change_in_pct_black +
                                                 county_change_in_pct_hispanic +
                                                 county_change_in_pct_asian, 
                                               data = diversification_YOUGOV_white_2020_presidential,
                                               weights = weight)
summary(controlling_for_all_STUDY1_BLACK_VIOLENCE)



####################################################################
####################################################################
#### APPENDIX E1
## Controlling for all counties for each interaction, hispanic county
####################################################################
####################################################################
controlling_for_all_STUDY1_hispanic_VIOLENCE<- lm(Support_political_violence ~ 
                                                 diversification_treatment*county_change_in_pct_hispanic +
                                                 PID + GENDER + 
                                                 EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                 county_population_change +
                                                 pres_raw_county_vote_totals_two_party +
                                                 raw_county_vote_totals 
                                               +county_change_in_pct_black +
                                                 county_change_in_pct_hispanic +
                                                 county_change_in_pct_asian, 
                                               data = diversification_YOUGOV_white_2020_presidential,
                                               weights = weight)
summary(controlling_for_all_STUDY1_hispanic_VIOLENCE)

####################################################################
####################################################################
#### APPENDIX E1
## Controlling for all counties for each interaction, asian county
####################################################################
####################################################################
controlling_for_all_STUDY1_asian_VIOLENCE<- lm(Support_political_violence ~ 
                                                    diversification_treatment*county_change_in_pct_asian +
                                                    PID + GENDER + 
                                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                                    county_population_change +
                                                    pres_raw_county_vote_totals_two_party +
                                                    raw_county_vote_totals 
                                                  +county_change_in_pct_black +
                                                    county_change_in_pct_hispanic +
                                                    county_change_in_pct_asian, 
                                                  data = diversification_YOUGOV_white_2020_presidential,
                                                  weights = weight)
summary(controlling_for_all_STUDY1_asian_VIOLENCE)

####################################################################
####################################################################
#### STUDY 1, Black
## APPENDIX C4.COUNT3
####################################################################
####################################################################

county_predictors_vote_trump<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_black*Purple_county +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_YOUGOV_white_2020_presidential,
                                  weights = weight)
summary(county_predictors_vote_trump)
interact_plot(county_predictors_vote_trump, 
              pred =county_change_in_pct_black, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, Black
## APPENDIX C4.COUNT3
####################################################################
####################################################################

county_predictors_vote_trump<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_hispanic*Purple_county +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_YOUGOV_white_2020_presidential,
                                  weights = weight)
summary(county_predictors_vote_trump)
interact_plot(county_predictors_vote_trump, 
              pred =county_change_in_pct_hispanic, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, Black
## APPENDIX C4.COUNT3
####################################################################
####################################################################
county_predictors_vote_trump<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_asian*Purple_county +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_YOUGOV_white_2020_presidential,
                                  weights = weight)
summary(county_predictors_vote_trump)
interact_plot(county_predictors_vote_trump, 
              pred =county_change_in_pct_asian, 
              modx = diversification_treatment, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")


####################################################################
####################################################################
#### STUDY 1, White
## APPENDIX C4.COUNT5
####################################################################
####################################################################

county_predictors_vote_trump_LUCID <- lm(white_nationalism_THREAT ~  
                                          diversification_prime* county_change_in_pct_white*Red_county
                                        + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                    county_population +
                                      county_population_change +
                                      pres_raw_county_vote_totals_two_party +
                                      raw_county_vote_totals, 
                                  data = diversification_LUCID_white_study2_2020,)
summary(county_predictors_vote_trump_LUCID)
interact_plot(county_predictors_vote_trump_LUCID, 
              pred =county_change_in_pct_white, 
              modx = diversification_prime, 
              mod2 = Red_county,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",
              linearity.check = T)


######## APPENDIX C4.COUNT7
county_predictors_vote_trump_LUCID<- lm(worse_if_a_rep_was_MURDERED ~  
                                          diversification_prime* county_change_in_pct_hispanic*Purple_county+
                                          PID + EDU  +
                                          IDEO_pre + INCOME + 
                                          GENDER   +
                                          county_population +
                                          county_population_change +
                                          pres_raw_county_vote_totals_two_party +
                                          raw_county_vote_totals 
                                        +county_change_in_pct_white, 
                                        data = diversification_LUCID_white_study2_2020,)
summary(county_predictors_vote_trump_LUCID)
interact_plot(county_predictors_vote_trump_LUCID, 
              pred =county_change_in_pct_hispanic, 
              modx = diversification_prime, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

### APPENDIX C4.COUNT6

county_predictors_vote_trump_LUCID<- lm(white_nationalism_THREAT ~  
                                          diversification_prime* county_change_in_pct_asian*Purple_county +
                                          PID + EDU  + IDEO_pre + INCOME + 
                                          GENDER   +
                                          county_population +
                                          county_population_change +
                                          pres_raw_county_vote_totals_two_party +
                                          raw_county_vote_totals, 
                                        data = diversification_LUCID_white_study2_2020,)
summary(county_predictors_vote_trump_LUCID)
interact_plot(county_predictors_vote_trump_LUCID, 
              pred =county_change_in_pct_asian, 
              modx = diversification_prime, 
              mod2 = Purple_county,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

#########
#####  STUDY 4 MERGING WITH PRESIDENTIAL COUNTY DATA
#########
prime_corrections_PNAS_white_study4_2020 = merge(x=prime_corrections_white_fight_WHITE,
                                                y=presidential_county_data_2020,
                                                by="COUNTY",all.x=TRUE)
prime_corrections_PNAS_white_study4_2020$Vote_Trump_positive_2020= (prime_corrections_PNAS_white_study4_2020$republican_raw_votes - 
                                                                      prime_corrections_PNAS_white_study4_2020$democratic_raw_votes)
# Red counties: Vote_Trump_positive_2020 > 2000
prime_corrections_PNAS_white_study4_2020$Red_county <- 
  ifelse(prime_corrections_PNAS_white_study4_2020$Vote_Trump_positive_2020 > 2000, 1, 0)

# Blue counties: Vote_Trump_positive_2020 < -2000
prime_corrections_PNAS_white_study4_2020$Blue_county <- 
  ifelse(prime_corrections_PNAS_white_study4_2020$Vote_Trump_positive_2020 < -2000, 1, 0)

# Purple counties: -2000 <= Vote_Trump_positive_2020 <= 2000
prime_corrections_PNAS_white_study4_2020$Purple_county <- 
  ifelse(prime_corrections_PNAS_white_study4_2020$Vote_Trump_positive_2020 >= -2000 & 
           prime_corrections_PNAS_white_study4_2020$Vote_Trump_positive_2020 <= 2000, 1, 0)




study4_PNAS_violence_black_counties = lm(violence_necessary ~ 
                                 correct_poc *county_change_in_pct_asian*Red_county 
                               + violence_necessary_pre 
                               + PID_pre + EDU + INCOME,
                               data = prime_corrections_PNAS_white_study4_2020)
summary(study4_PNAS_violence_black_counties)
interact_plot(study4_PNAS_violence_black_counties, 
              pred =county_change_in_pct_asian, 
              modx = correct_poc, 
              mod2 = Blue_county,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


#########################
#########################
#########################
##############
### APPENDIX C4.PID1
#########################
#########################
#########################
county_predictors_vote_PID<- lm(Support_political_violence ~ 
                                  diversification_treatment*county_change_in_pct_black*PID +
                                  PID + GENDER + 
                                  EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                  county_population_change ,
                                data = diversification_YOUGOV_white_2020_presidential,
                                weights = weight)
summary(county_predictors_vote_PID)
interact_plot(county_predictors_vote_PID, 
              pred =PID, 
              modx = diversification_treatment, 
              mod2 = county_change_in_pct_black,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

county_predictors_vote_PID2<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_asian*PID +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_YOUGOV_white_2020_presidential,
                                  weights = weight)
summary(county_predictors_vote_PID2)
interact_plot(county_predictors_vote_PID2, 
              pred =PID, 
              modx = diversification_treatment, 
              mod2 = county_change_in_pct_asian,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")



## Study 2

county_predictors_vote_trump_LUCID <- lm(insurrectionist_sentiment_POST ~  
                                           diversification_prime* county_change_in_pct_white*PID
                                         + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                           county_population  +
                                           county_population_change +
                                           pres_raw_county_vote_totals_two_party +
                                           raw_county_vote_totals, 
                                         data = diversification_LUCID_white_study2_2020,)

summary(county_predictors_vote_trump_LUCID)
interact_plot(county_predictors_vote_trump_LUCID, 
              pred =PID, 
              modx = diversification_prime, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)








# study 4

study4_PNAS_violence_white_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_white*PID_pre
                                         + violence_necessary_pre
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_PNAS_white_study4_2020)
summary(study4_PNAS_violence_white_counties)


study4_PNAS_violence_black_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_black*PID_pre
                                         + violence_necessary_pre
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_PNAS_white_study4_2020)
summary(study4_PNAS_violence_black_counties)

study4_PNAS_violence_hispanic_counties = lm(violence_necessary ~ 
                                              correct_poc *county_change_in_pct_hispanic*PID_pre
                                            + violence_necessary_pre
                                            + PID_pre + EDU + INCOME,
                                            data = prime_corrections_PNAS_white_study4_2020)
summary(study4_PNAS_violence_hispanic_counties)


study4_PNAS_violence_asian_counties = lm(violence_necessary ~ 
                                           correct_poc *county_change_in_pct_asian*PID_pre
                                         + violence_necessary_pre
                                         + PID_pre + EDU + INCOME,
                                         data = prime_corrections_PNAS_white_study4_2020)
summary(study4_PNAS_violence_asian_counties)

interact_plot(study4_PNAS_violence_black_counties, 
              pred =PID_pre, 
              modx = correct_poc, 
              mod2 = county_change_in_pct_white,
              interval = T ,
              x.label = "",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


################################################
################################################
################ WHITE #########################
################################################
################################################
r1 <- lm(Support_political_violence ~ 
           diversification_treatment * county_change_in_pct_white.x * white_composition_prior +
           PID + GENDER + 
           EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
           county_population_change.x,
         data = diversification_YOUGOV_proportion2,
         weights = weight)
summary(r1)

interact_plot(r1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",
)

r2 <- lm(Support_political_violence ~ 
           diversification_treatment * county_change_in_pct_black.x * white_composition_prior +
           PID + GENDER + 
           EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
           county_population_change.x,
         data = diversification_YOUGOV_proportion2,
         weights = weight)
summary(r2)
interact_plot(r2, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",
)
r3 <- lm(Support_political_violence ~ 
           diversification_treatment * county_change_in_pct_hispanic.x * white_composition_prior +
           PID + GENDER + 
           EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
           county_population_change.x,
         data = diversification_YOUGOV_proportion2,
         weights = weight)
summary(r3)
interact_plot(r3, 
              pred =county_change_in_pct_hispanic.x, 
              modx = diversification_treatment, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
r4 <- lm(Support_political_violence ~ 
           diversification_treatment * county_change_in_pct_asian.x * white_composition_prior +
           PID + GENDER + 
           EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
           county_population_change.x,
         data = diversification_YOUGOV_proportion2,
         weights = weight)
summary(r4)
interact_plot(r4, 
              pred =county_change_in_pct_asian.x, 
              modx = diversification_treatment, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
################################################
################################################
################ White #########################
################################################
################################################

black_comp_prior_study1 <- lm(Support_political_violence ~ 
                                diversification_treatment * county_change_in_pct_black.x * black_composition_prior +
                                PID + GENDER + 
                                EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                                county_population_change.x,
                              data = diversification_YOUGOV_proportion2,
                              weights = weight)
summary(black_comp_prior_study1)



urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment * county_change_in_pct_white.x * above_97_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_97_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",
)
urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_white.x * above_95_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_95_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
white_90_s1 <- lm(Support_political_violence ~ 
                    diversification_treatment*county_change_in_pct_white.x * above_90_white +
                    PID + GENDER + 
                    EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                    county_population_change.x,
                  data = diversification_YOUGOV_proportion1,
                  weights = weight)
summary(white_90_s1)
interact_plot(white_90_s1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_white.x * above_85_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_85_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_white.x * above_80_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_80_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_white.x * above_75_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = above_75_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

major_min_flip_s1 <- lm(Support_political_violence ~ 
                          diversification_treatment*county_change_in_pct_white.x * white_majority_minority_flip +
                          PID + GENDER + 
                          EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                          county_population_change.x,
                        data = diversification_YOUGOV_proportion1,
                        weights = weight)
summary(major_min_flip_s1)
interact_plot(major_min_flip_s1, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_treatment, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment * county_change_in_pct_black.x * above_97_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_97_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",
)
urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_black.x * above_95_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_95_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

white_90_s1_black <- lm(Support_political_violence ~ 
                          diversification_treatment*county_change_in_pct_black.x * above_90_white +
                          PID + GENDER + 
                          EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                          county_population_change.x,
                        data = diversification_YOUGOV_proportion1,
                        weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_black.x * above_85_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_85_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_black.x * above_80_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_80_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

urpol1 <- lm(Support_political_violence ~ 
               diversification_treatment*county_change_in_pct_black.x * above_75_white +
               PID + GENDER + 
               EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
               county_population_change.x,
             data = diversification_YOUGOV_proportion1,
             weights = weight)
summary(urpol1)
interact_plot(urpol1, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = above_75_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

major_min_flip_s1_black <- lm(Support_political_violence ~ 
                                diversification_treatment*county_change_in_pct_black.x * white_majority_minority_flip +
                                PID + GENDER + 
                                EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                                county_population_change.x,
                              data = diversification_YOUGOV_proportion1,
                              weights = weight)
summary(major_min_flip_s1_black)
interact_plot(major_min_flip_s1_black, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_treatment, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

################################################
################################################
################ HISPANIC #########################
################################################
################################################
white_90_s1_hispanic <- lm(Support_political_violence ~ 
                             diversification_treatment*county_change_in_pct_hispanic.x * above_90_white +
                             PID + GENDER + 
                             EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                             county_population_change.x,
                           data = diversification_YOUGOV_proportion1,
                           weights = weight)
summary(white_90_s1_hispanic)
interact_plot(white_90_s1_hispanic, 
              pred =county_change_in_pct_hispanic.x, 
              modx = diversification_treatment, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
major_min_flip_s1_hispanic <- lm(Support_political_violence ~ 
                                   diversification_treatment*county_change_in_pct_hispanic.x * white_majority_minority_flip +
                                   PID + GENDER + 
                                   EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                                   county_population_change.x,
                                 data = diversification_YOUGOV_proportion1,
                                 weights = weight)
summary(major_min_flip_s1_hispanic)
interact_plot(major_min_flip_s1_hispanic, 
              pred =county_change_in_pct_hispanic.x, 
              modx = diversification_treatment, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


################################################
################################################
################ ASIAN #########################
################################################
################################################
white_90_s1_asian <- lm(Support_political_violence ~ 
                          diversification_treatment*county_change_in_pct_asian.x * above_90_white +
                          PID + GENDER + 
                          EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                          county_population_change.x,
                        data = diversification_YOUGOV_proportion1,
                        weights = weight)
summary(white_90_s1_asian)
interact_plot(white_90_s1_asian, 
              pred =county_change_in_pct_asian.x, 
              modx = diversification_treatment, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "PID",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

major_min_flip_s1_asian <- lm(Support_political_violence ~ 
                                diversification_treatment*county_change_in_pct_asian.x * white_majority_minority_flip +
                                PID + GENDER + 
                                EDU  + VOTER_REGISTERED_ALL + INCOME  +county_population.x +
                                county_population_change.x,
                              data = diversification_YOUGOV_proportion1,
                              weights = weight)
summary(major_min_flip_s1_asian)

####################################################################
####################################################################
#### STUDY 2, PID moderator, APPENDIX C4.COUNT
####################################################################
####################################################################
diversification_LUCID_white_study2_w_majority_minority_flip = merge(x=diversification_LUCID_white_study2_2020,
                                                                    y=res,by="zip",all.x=T)
table(diversification_LUCID_white_study2_w_majority_minority_flip$white_majority_minority_flip)
diversification_LUCID_white_study2_w_majority_minority_flip = merge(x=diversification_LUCID_white_study2_2020,
                                                                    y=res,by="zip",all.x=T)
table(diversification_LUCID_white_study2_w_majority_minority_flip$white_composition_prior)
table(diversification_LUCID_white_study2_w_majority_minority_flip$black_composition_prior)

murder_white_90_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                           diversification_prime* county_change_in_pct_white.x*white_composition_prior
                         + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                           county_population.x  + 
                           county_population_change.x +
                           pres_raw_county_vote_totals_two_party +
                           raw_county_vote_totals, 
                         data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_white_90_s2)
interact_plot(murder_white_90_s2, 
              pred = county_change_in_pct_white.x, 
              modx = diversification_prime, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


murder_white_maj_min_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                diversification_prime* county_change_in_pct_white.x*white_majority_minority_flip
                              + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                county_population.x  + 
                                county_population_change.x +
                                pres_raw_county_vote_totals_two_party +
                                raw_county_vote_totals, 
                              data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_white_maj_min_s2)
interact_plot(murder_white_maj_min_s2, 
              pred =county_change_in_pct_white.x, 
              modx = diversification_prime, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
murder_black_white_90_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                 diversification_prime* county_change_in_pct_black.x*above_90_white
                               + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                 county_population.x  + 
                                 county_population_change.x +
                                 pres_raw_county_vote_totals_two_party +
                                 raw_county_vote_totals, 
                               data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_black_white_90_s2)
interact_plot(murder_black_white_90_s2, 
              pred = county_change_in_pct_black.x, 
              modx = diversification_prime, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
murder_black_maj_min_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                diversification_prime* county_change_in_pct_black.x*white_majority_minority_flip
                              + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                county_population.x  + 
                                county_population_change.x +
                                pres_raw_county_vote_totals_two_party +
                                raw_county_vote_totals, 
                              data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_black_maj_min_s2)
interact_plot(murder_black_maj_min_s2, 
              pred =county_change_in_pct_black.x, 
              modx = diversification_prime, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


murder_hispanic_white_90_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                    diversification_prime* county_change_in_pct_hispanic.x*above_90_white
                                  + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                    county_population.x  + 
                                    county_population_change.x +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_hispanic_white_90_s2)
interact_plot(murder_hispanic_white_90_s2, 
              pred =county_change_in_pct_hispanic.x, 
              modx = diversification_prime, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
murder_hispanic_maj_min_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                   diversification_prime* county_change_in_pct_asian.x*white_majority_minority_flip
                                 + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                   county_population.x  + 
                                   county_population_change.x +
                                   pres_raw_county_vote_totals_two_party +
                                   raw_county_vote_totals, 
                                 data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_hispanic_maj_min_s2)
interact_plot(murder_hispanic_maj_min_s2, 
              pred = county_change_in_pct_asian.x,
              modx = diversification_prime, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

murder_hispanic_white_90_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                    diversification_prime* county_change_in_pct_hispanic.x*above_90_white
                                  + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                    county_population.x  + 
                                    county_population_change.x +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_hispanic_white_90_s2)
interact_plot(murder_hispanic_white_90_s2, 
              pred =county_change_in_pct_hispanic.x, 
              modx = diversification_prime, 
              mod2 = above_90_white,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
murder_asian_maj_min_s2 <- lm(worse_if_a_rep_was_MURDERED ~  
                                diversification_prime* county_change_in_pct_asian.x*white_majority_minority_flip
                              + PID + EDU  + IDEO_pre + INCOME + GENDER  +
                                county_population.x  + 
                                county_population_change.x +
                                pres_raw_county_vote_totals_two_party +
                                raw_county_vote_totals, 
                              data = diversification_LUCID_white_study2_w_majority_minority_flip,)
summary(murder_asian_maj_min_s2)
interact_plot(murder_asian_maj_min_s2, 
              pred = county_change_in_pct_asian.x,
              modx = diversification_prime, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)





####################################################################
####################################################################
#### STUDY 3, APPENDIX C4.COUNT ########### 
####################################################################
####################################################################
state_threat_white_only_w_majority_minority_flip = merge(x=state_threat_white_only1_2020_election,
                                                         y=res,by="zip",all.x=T)
table(state_threat_white_only_w_majority_minority_flip$black_composition_prior)

state_threat_violence_maj_min_w <- lm(Violence_support ~national_threat*county_change_in_pct_white.x*white_majority_minority_flip
                                      + Violence_support_pre 
                                      + EDU + PID + INCOME + GENDER,
                                      data = state_threat_white_only_w_majority_minority_flip)
summary(state_threat_violence_maj_min_w)
interact_plot(state_threat_violence_maj_min_w, 
              pred =county_change_in_pct_white.x, 
              modx = national_threat, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Red County",
              y.label = "Support for Political Violence",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

state_threat_violence_maj_min_b <- lm(Violence_support ~national_threat*county_change_in_pct_black.x*white_majority_minority_flip
                                      + Violence_support_pre 
                                      + EDU + PID + INCOME + GENDER,
                                      data = state_threat_white_only_w_majority_minority_flip)
summary(state_threat_violence_maj_min_b)
interact_plot(state_threat_violence_maj_min_b, 
              pred =county_change_in_pct_black.x, 
              modx = national_threat, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Red County",
              y.label = "Support for Political Violence",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
state_threat_violence_maj_min_h <- lm(Violence_support ~national_threat*county_change_in_pct_hispanic.x*white_majority_minority_flip
                                      + Violence_support_pre 
                                      + EDU + PID + INCOME + GENDER,
                                      data = state_threat_white_only_w_majority_minority_flip)
summary(state_threat_violence_maj_min_h)
interact_plot(state_threat_violence_maj_min_h, 
              pred =county_change_in_pct_hispanic.x, 
              modx = national_threat, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Red County",
              y.label = "Support for Political Violence",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
state_threat_violence_maj_min_a <- lm(Violence_support ~national_threat*county_change_in_pct_asian.x*white_majority_minority_flip
                                      + Violence_support_pre 
                                      + EDU + PID + INCOME + GENDER,
                                      data = state_threat_white_only_w_majority_minority_flip)
summary(state_threat_violence_maj_min_a)
interact_plot(state_threat_violence_maj_min_a, 
              pred =county_change_in_pct_asian.x, 
              modx = national_threat, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Red County",
              y.label = "Support for Political Violence",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

p=huxreg(state_threat_violence_maj_min_w,
         state_threat_violence_maj_min_b,
         state_threat_violence_maj_min_h,
         state_threat_violence_maj_min_a)



####################################################################
####################################################################
########################### STUDY 4 ################################
####################################################################
####################################################################
prime_corrections_PNAS_white_study4_2020$zip= (prime_corrections_PNAS_white_study4_2020$zip_code)
prime_corrections_white_study4_w_majority_minority_flip = merge(x=prime_corrections_PNAS_white_study4_2020,
                                                                y=res,by="zip",all.x=T)
table(prime_corrections_white_study4_w_majority_minority_flip$white_composition_prior)

study4_threat_violence_maj_min_w = lm(violence_necessary ~ 
                                        correct_poc *county_change_in_pct_white.x*white_majority_minority_flip 
                                      + violence_necessary_pre 
                                      + PID_pre + EDU + INCOME,
                                      data = prime_corrections_white_study4_w_majority_minority_flip)
summary(study4_threat_violence_maj_min_w)
interact_plot(study4_threat_violence_maj_min_w, 
              pred =county_change_in_pct_white.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

study4_threat_violence_maj_min_b = lm(violence_necessary ~ 
                                        correct_poc *county_change_in_pct_black.x*white_majority_minority_flip 
                                      + violence_necessary_pre 
                                      + PID_pre + EDU + INCOME,
                                      data = prime_corrections_white_study4_w_majority_minority_flip)
summary(study4_threat_violence_maj_min_b)
interact_plot(study4_threat_violence_maj_min_b, 
              pred =county_change_in_pct_black.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

study4_threat_violence_maj_min_h = lm(violence_necessary ~ 
                                        correct_poc *county_change_in_pct_hispanic.x*white_majority_minority_flip 
                                      + violence_necessary_pre 
                                      + PID_pre + EDU + INCOME,
                                      data = prime_corrections_white_study4_w_majority_minority_flip)
summary(study4_threat_violence_maj_min_h)
interact_plot(study4_threat_violence_maj_min_h, 
              pred =county_change_in_pct_hispanic.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


study4_threat_violence_maj_min_a = lm(violence_necessary ~ 
                                        correct_poc *county_change_in_pct_asian.x*white_majority_minority_flip 
                                      + violence_necessary_pre 
                                      + PID_pre + EDU + INCOME,
                                      data = prime_corrections_white_study4_w_majority_minority_flip)
summary(study4_threat_violence_maj_min_a)
interact_plot(study4_threat_violence_maj_min_a, 
              pred =county_change_in_pct_asian.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


uurple1 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_white.x*white_composition_prior 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple1)
interact_plot(uurple1, 
              pred =county_change_in_pct_white.x, 
              modx = correct_poc, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Support for Political Violence",
              
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


uurple2 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_black.x*white_composition_prior 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple2)
interact_plot(uurple2, 
              pred =county_change_in_pct_black.x, 
              modx = correct_poc, 
              mod2 = white_composition_prior,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Support for Political Violence",
              
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)


uurple1 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_white.x*white_majority_minority_flip 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple1)
interact_plot(uurple1, 
              pred =county_change_in_pct_white.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

uurple2 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_black.x*white_majority_minority_flip 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple2)
interact_plot(uurple2, 
              pred =county_change_in_pct_black.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

uurple3 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_hispanic.x*white_majority_minority_flip 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple3)
interact_plot(uurple3, 
              pred =county_change_in_pct_hispanic.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)

uurple4 = lm(violence_necessary ~ 
               correct_poc *county_change_in_pct_asian.x*white_majority_minority_flip 
             + violence_necessary_pre 
             + PID_pre + EDU + INCOME,
             data = prime_corrections_white_study4_w_majority_minority_flip)
summary(uurple4)
interact_plot(uurple4, 
              pred =county_change_in_pct_asian.x, 
              modx = correct_poc, 
              mod2 = white_majority_minority_flip,
              interval = T ,
              x.label = "Change in white population percentage, county-level",
              y.label = "Fearful Perception of National Racial Demographic Change",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright",)
p<- huxreg(uurple1,
           uurple2,
           uurple3,
           uurple4)
quick_docx(p)



############################################## 
##### MERGING on zip for measuring rural and urban
##############################################

census_2020_urban_rural = read_excel("file-path")

diversification_YOUGOV_white_2020_presidential$zip = (diversification_YOUGOV_white_2020_presidential$inputzip)
diversification_YOUGOV_white_2020_presidential_WITH_UA2020 = merge(x=diversification_YOUGOV_white_2020_presidential,
                        y=census_2020_urban_rural, by="zip",all.x=TRUE)

# Rural counties, over 65 percent rural
diversification_YOUGOV_white_2020_presidential_WITH_UA2020$Rural <- 
  ifelse(diversification_YOUGOV_white_2020_presidential_WITH_UA2020$POPPCT_RUR > 0.65, 1, 0)

table(diversification_YOUGOV_white_2020_presidential_WITH_UA2020$Rural)


####################################################################
####################################################################
#### STUDY 1, White
## APPENDIX E
####################################################################
####################################################################

county_predictors_vote_trump_urban<- lm(Support_political_violence ~ 
                                    diversification_treatment*county_change_in_pct_white*POPPCT_RUR +
                                    PID + GENDER + 
                                    EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                    county_population_change +
                                    pres_raw_county_vote_totals_two_party +
                                    raw_county_vote_totals, 
                                  data = diversification_YOUGOV_white_2020_presidential_WITH_UA2020,
                                  weights = weight)
summary(county_predictors_vote_trump_urban)
interact_plot(county_predictors_vote_trump_urban, 
              pred =county_change_in_pct_white, 
              modx = diversification_treatment, 
              mod2 = POPPCT_RUR,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

####################################################################
####################################################################
#### STUDY 1, Black
## APPENDIX E
####################################################################
####################################################################
county_predictors_vote_trump_urban_black<- lm(Support_political_violence ~ 
                                          diversification_treatment*county_change_in_pct_black*POPPCT_RUR +
                                          PID + GENDER + 
                                          EDU  + VOTER_REGISTERED_ALL + INCOME + county_population +
                                          county_population_change +
                                          pres_raw_county_vote_totals_two_party +
                                          raw_county_vote_totals, 
                                        data = diversification_YOUGOV_white_2020_presidential_WITH_UA2020,
                                        weights = weight)
summary(county_predictors_vote_trump_urban_black)
interact_plot(county_predictors_vote_trump_urban_black, 
              pred =county_change_in_pct_white, 
              modx = diversification_treatment, 
              mod2 = POPPCT_RUR,
              interval = T ,
              x.label = "County Change",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
              colors = "CUD Bright")

diversification_YOUGOV_white_2020_presidential$ZIP = diversification_YOUGOV_white_2020_presidential$zip
prime_corrections_white_fight_WHITE$ZIP = prime_corrections_white_fight_WHITE$zip_code
state_threat_white_only1_2020_election$ZIP = state_threat_white_only1_2020_election$zip
chronic_threat_LUCID_white$ZIP = chronic_threat_LUCID_white$zip_code

write.csv(study1_PNAS_yougov_WHITE, "study1_PNAS_white.csv")
write.csv(study2_PNAS_LUCID_white, "study2_PNAS_white.csv")
write.csv(state_threat_white_change_white_only1, "study3_PNAS_white_only.csv")
write.csv(prime_corrections_white_fight_WHITE, "study4_PNAS_white_only.csv")








